#' Estimating the distribution of the sum of dependent variables
#' 
#'  
#'  @param data contains the data
#'  @param alpha specifies the alpha value
#'  @keywords aep
#'  @export
#'  @examples
#'  par(ask=TRUE)
#'  par(mfrow=c(1,1))
#'  nb_variables <- 2
#'  nb_observations <- 30000  
#'  my_data = matrix(runif(nb_observations * nb_variables), c(nb_observations, nb_variables))
#'  my_data <- my_data * 50
#'  my_data <- data.frame(my_data)
#'  plot(my_data, xlim=c(0,50), ylim=c(0,50))
#'  data_sum <- my_data[,1] + my_data[,2]
#'  print("Frequency of observing the sum of the two variables to be <= 25")
#'  print(sum(data_sum <= 25) / length(data_sum))
#'  result <- AEP.d(data=my_data, vsum=25, iteration=100)
#'  result

AEP.d <- function(data, alpha, iteration=1, vsum) {
  if (missing(data) || missing(vsum)) stop("syntax error")
  if (is.data.frame(data) == FALSE) stop("the parameter 'data' must be of type 'data.frame'")    
  if (is.numeric(vsum) == FALSE) stop("the parameter 'vsum' must be of type 'numeric'")        
  if (missing(alpha)) alpha <- 2 / (dim(data)[2] + 1)
  else if (is.numeric(alpha) == FALSE) stop("the parameter 'alpha' must be of type 'numeric'")      
  if (alpha < 1 / dim(data)[2] || alpha >= 1) stop("the parameter 'alpha' must be in [1/d, 1)")       
  if (is.numeric(iteration) == FALSE) stop("the parameter 'iteration' must be of type 'numeric'") 	 
  if (iteration < 1) stop("the parameter 'iteration' must be >= 1")	
  
  frame <- as.aep.frame(data, alpha, iteration) 
  resolveAEP(frame, vsum)
}

AEP.f <- function(frame, vsum) {    
  if (missing(frame) || missing(vsum)) stop("syntax error")
  if (class(frame) != "AEP.frame") stop("the parameter 'frame' must be of type 'AEP.frame'")
  if (is.numeric(vsum) == FALSE) stop("the parameter 'vsum' must be of type 'numeric'")
  resolveAEP(frame, vsum)
}


